﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace TA.Turbo.Controls
{
	public abstract class BasePopupControl : BaseComplexControl, INamingContainer
	{
		#region PUBLIC
		public BasePopupControl()
		{
			_body = new SingleInstanceTemplate(this, "Body");
		}
		public bool Shown { get; set; }
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public SingleInstanceTemplate Body { get { return _body; } }
		public string GetShowHideScript(bool visibility)
		{
			return string.Format("TA.Turbo.Common.SetControlProperty(document.getElementById('{0}'), 'Shown', {1});",
				ClientID, visibility.ToString().ToLower());
		}
		#endregion
		#region INTERNAL
		SingleInstanceTemplate _body;
		protected override string GetSerializedProperty(string propertyName)
		{
			switch (propertyName)
			{
				case "Shown": return Shown.ToString().ToLower();
			}
			return base.GetSerializedProperty(propertyName);
		}
		protected override void SetSerializedProperty(string propertyName, string value)
		{
			switch (propertyName)
			{
				case "Shown": Shown = bool.Parse(value); return;
			}
			base.SetSerializedProperty(propertyName, value);
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			TurboPage.Current.Includes.Add(new ScriptResourceInclude
			{
				TypeName = this.GetType().FullName,
				Resource = "TA.Turbo.Resources.JS.BasePopupControl.js"
			});

		}
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			Style[HtmlTextWriterStyle.Display] = Shown ? "" : "none";

			base.AddAttributesToRender(writer);
		}
		protected override void RenderChildren(System.Web.UI.HtmlTextWriter writer)
		{
			_body.Container.RenderControl(writer);
		}
		#endregion
	}
}
